<template>
  <el-dialog :model-value="showAnswerSkillDialog" :title="props.editInfo.id ? '编辑分类' : '新建分类'" width="30%"
    @close="resetForm" :close-on-click-modal="false">
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="车型" prop="carIds">
        <el-select v-model="form.carIds" multiple placeholder="请选择" style="width: 100%" :disabled="isEdit">
          <el-option v-for="item in carList" :key="item.value" :label="`${item.label}(${item.tag})`"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="技巧标题" prop="name">
        <el-input v-model="form.name" placeholder="请输入技巧标题" />
      </el-form-item>
      <el-form-item label="关键字" prop="label">
        <el-input v-model="form.label" placeholder="">
          <template #append>
            <el-button @click="showKeyWordVisible = true">选择关键字</el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="描述" prop="subhead">
        <el-input v-model="form.subhead" type="textarea" placeholder="请输入描述" :autosize="{ minRows: 4, maxRows: 8 }" />
      </el-form-item>
      <!-- <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" :rows="3"></el-input>
      </el-form-item> -->
      <el-form-item label="用户权限" prop="isPay">
        <div>
          <el-radio-group v-model="form.isPay">
            <el-radio value="0">全部用户可看</el-radio>
            <el-radio value="1">仅限VIP会员可看</el-radio>
          </el-radio-group>
          <div style="color: #ccc;font-size: 12px;">选择“仅限VIP会员可看”时请先设置好会员权益</div>
        </div>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="editLoading" @click="submitForm(ruleFormRef)">确认</el-button>
        <el-button @click="resetForm">取消</el-button>
      </div>
    </template>

    <key-word-selected-dialog v-if="showKeyWordVisible" :show-key-word-visible="showKeyWordVisible" :text="form.name"
      @close-dialog="handleCloseKeyWordDialog"></key-word-selected-dialog>
  </el-dialog>
</template>

<script setup lang="ts">
import { operationCategory } from '@/api/questionbanklabel'
import KeyWordSelectedDialog from './KeyWordSelectedDialog.vue'
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { getcarmodellist } from '@/api/questionbank'
import { log } from 'node:console'

const props = defineProps({
  showAnswerSkillDialog: {
    type: Boolean
  },
  editInfo: {
    type: Object,
    default: () => {
      return {
        id: ''
      }
    }
  }
})

const ruleFormRef = ref<FormInstance>()

interface Form {
  name: string
  img: string
  imgTwo: string
  parentId: number
  carIds: any
  label: string
  pageUrlType: string
  remark: string
  subhead: string
  categoryIds: string
}

const form = reactive({
  name: '',
  pageUrlType: '',
  label: '',
  carIds: [],
  parentId: 0,
  remark: '',
  status: 1,
  subhead: '',
  categoryIds: '',
  isPay: '1'
})

const rules = reactive<FormRules<Form>>({
  name: [{ required: true, message: '不能为空', trigger: 'blur' }],
  carIds: [{ required: true, message: '不能为空', trigger: 'change', type: 'array' }],
  label: [{ required: true, message: '不能为空', trigger: 'blur' }],
  remark: [{ required: true, message: '不能为空', trigger: 'blur' }],
  pageUrlType: [{ required: true, message: '不能为空', trigger: 'blur' }]
})

const editLoading = ref(false)
const showKeyWordVisible = ref(false)

const isEdit = computed(() => {
  return props.editInfo.children && props.editInfo.children.length > 0
})

interface carLists {
  label: string
  value: string
  tag: string
}
const carList = ref<Array<carLists>>([])

const emit = defineEmits(['closeDialog'])

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      if (props.editInfo.id) {
        handleEdit()
      } else {
        handleAdd()
      }
    } else {
    }
  })
}

async function handleAdd() {
  try {
    editLoading.value = true
    console.log(form.status, "form.status==");

    const res = await operationCategory.newAddCategory({
      ...form,
      carIds: form.carIds.join(',')
    })
    if (res.code === '00000') {
      emit('closeDialog', true, form.status)
      ElMessage.success('新增成功')
    }
  } catch (e) {
  } finally {
    editLoading.value = false
  }
}

async function handleEdit() {
  try {
    editLoading.value = true
    const res = await operationCategory.eidtCategory(
      {
        ...form,
        carIds: form.carIds.join(',')
      },
      props.editInfo.id
    )
    if (res.code === '00000') {
      emit('closeDialog', true, form.status)
      ElMessage.success('编辑成功')
    }
  } catch (e) {
  } finally {
    editLoading.value = false
  }
}

const resetForm = () => {
  emit('closeDialog')
}

function handleCloseKeyWordDialog(value: any) {
  if (value) {
    form.label = value.join(',')
  }
  showKeyWordVisible.value = false
}

//车型下拉接口
const getcarmodellistinfo = async () => {
  let obj = {}
  const res = await getcarmodellist.getcarmodelId(obj)
  if (res.code == 0) {
    carList.value = res.data
  }
}

onMounted(() => {
  if (props.editInfo.id) {
    form.name = props.editInfo.name
    form.carIds = props.editInfo.carIds.split(',')
    form.label = props.editInfo.label
    form.remark = props.editInfo.remark
    form.status = props.editInfo.status
    form.isPay = String(props.editInfo.isPay)
    form.subhead = props.editInfo.subhead
  }
  form.pageUrlType = props.editInfo.pageUrlType
  form.parentId = props.editInfo.parentId
  form.categoryIds = props.editInfo.categoryIds
  getcarmodellistinfo()
})
</script>

<style scoped lang="scss">
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 180px;
  height: 180px;
  line-height: 180px;
  text-align: center;
}

.avatar {
  width: 180px;
  height: 180px;
  display: block;
}
</style>
